//
//  ASDatePickerView.m
//  AttendenceSys
//
//  Created by ld on 16/1/23.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASDatePickerView.h"

@implementation ASDatePickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //初始化数据
        //[self initData];
        [self setBackgroundColor:[UIColor whiteColor]];
        
        //初始化标题视图
        [self initTitleView];
        
        //初始化日期选择
        [self initDatePickerView];
        
        
    }
    return self;
}

-(void)initDatePickerView
{
    _datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, _titleView.frame.size.height, _titleView.frame.size.width, self.frame.size.height-_titleView.frame.size.height)];
    CGPoint center = _datePickerView.center;
    center.x = self.center.x;
    _datePickerView.center = center;
    //背景
    [self setDatePickerBackgroundViewColor];
    _datePickerView.datePickerMode = UIDatePickerModeDate;
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    self.clipsToBounds = NO;
    _datePickerView.clipsToBounds = NO;
    //    [[[UIApplication sharedApplication] keyWindow] addSubview:_datePickerView];
    [self addSubview:_datePickerView];
    
    NSDate *date1 = nil;
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *nowDate = [NSDate date];
    NSTimeInterval interval = 0; //30分钟
    date1 = [nowDate initWithTimeIntervalSinceNow:+interval];
    [_datePickerView setDate:date1 animated:YES];
    [self addSubview:_datePickerView];
    
}

-(void)initTitleView
{
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 46)];
    [_titleView setBackgroundColor:[UIColor colorWithRed:229.0/255.0 green:229.0/255.0 blue:229.0/255.0 alpha:1.0]];
    UIButton* cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 8, 46, 30)];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setBackgroundColor:[UIColor clearColor]];
    [cancleBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [cancleBtn.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:cancleBtn];
    
    _editTimeLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 15, 80, 30)];
    [_editTimeLab setFont:[UIFont fontWithName:@"Arial" size:16.0f]];
    _editTimeLab.center = _titleView.center;
    [_editTimeLab setTextColor:[UIColor grayColor]];
    _editTimeLab.textAlignment = NSTextAlignmentCenter;
    [_titleView addSubview:_editTimeLab];
    
    UIButton* okBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-66, 8, 46, 30)];
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn setBackgroundColor:[UIColor clearColor]];
    [okBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [okBtn.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [okBtn addTarget:self action:@selector(ok) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:okBtn];
    [self addSubview:_titleView];
    
}

//取消
-(void)cancle
{
    if (_delegate && [_delegate respondsToSelector:@selector(datePickerCancel)]) {
        [_delegate datePickerCancel];
    }
    [self removeFromSuperview];
}

//确定
-(void)ok
{
    NSDate* date = nil;
    NSTimeInterval interval = 8*60*60; //8小时
    date = [_datePickerView.date initWithTimeInterval:interval sinceDate:_datePickerView.date];
    
    NSString *dateFromData = [NSString stringWithFormat:@"%@", date];
    
    NSString *selectdate = [dateFromData substringWithRange:NSMakeRange(0, 16)];
    
    if (_delegate && [_delegate respondsToSelector:@selector(dataPickSelect:selectDate:)])
    {
        [_delegate dataPickSelect:_iTag selectDate:selectdate];
    }
    
    [self removeFromSuperview];
    
}

-(void)setDatePickerBackgroundViewColor
{
    BOOL DeviceVersion = [[UIDevice currentDevice].systemVersion floatValue] >= 7.0;
    if (!DeviceVersion){
        //改背景色===================================================================
        _datePickerView.backgroundColor = [UIColor clearColor];
        
        UIView *v = [[_datePickerView subviews] objectAtIndex:0];
        
        for (int i = 0; i < [[v subviews] count]; ++i){
            if ( i == 0 || i == 4 || i == 10 || i == 16 || i == 22){
                //改变最外层的背景   //去掉最大的框
                UIView *tempView = [[v subviews] objectAtIndex:i ];
                tempView.backgroundColor = [UIColor whiteColor];
            }else if (i == 2 || i == 3 || i == 5 || i == 8 || i == 9 || i == 11 || i == 14 || i == 15 || i == 17 || i == 20 || i == 21 || i == 23 || i == [[v subviews] count] - 1 || i == [[v subviews] count] - 2){
                UIView *tempView = [[v subviews] objectAtIndex:i];
                tempView.alpha = 0.0;
            }else{
                UIView *tempView = [[v subviews] objectAtIndex:i ];
                tempView.alpha = 0.6;
            }
        }
        
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
